package com.yc.dygang.dao;

import com.yc.common.util.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MovieDao {

    public List<Map<String,Object>> queryNews(String category){
        String sql = "select * from dyg_movie" +
                " where category=?" +
                " order by create_date desc" +
                " limit 0,16";
        List<Map<String, Object>> list = DBHelper.selectListMap(sql,category);
        return list;
    }

    public Map<String,Object> selectById(String id){
        String sql = "select * from dyg_movie where id=?";
        return DBHelper.selectOneMap(sql,id);
    }

    public List<Map<String,Object>> queryNews1(String category){
        String sql = "select * from dyg_movie" +
                " where category=?" +
                " order by create_date desc" +
                " limit 0,6";
        List<Map<String, Object>> list = DBHelper.selectListMap(sql,category);
        return list;
    }

    public Map<String,Object> selectByNameAndCategory(String name,String category,String page){
        String sql = "select * from dyg_movie where 1=1";
        List<Object> params = new ArrayList<>();
        if (name != null && name.trim().isEmpty() == false){
            name = "%"+name.trim()+"%";
            sql += " and ( name like ? or name0 like ? or names like ? or intro like ? )";
            params.add(name);
            params.add(name);
            params.add(name);
            params.add(name);
        }
        if (category != null && category.trim().isEmpty() == false){
            sql += " and category = ?";
            params.add(category);
        }

        //查询总行数
        long count = DBHelper.count(sql, params.toArray());
        int anInt = Integer.parseInt(page);
        anInt = (anInt - 1) * 10;
        sql += " limit "+ anInt + " ,10";
        List<Map<String, Object>> list = DBHelper.selectListMap(sql,params.toArray());


        Map<String,Object> ret = new HashMap<>();
        ret.put("total",count);
        ret.put("data",list);
        return ret;
    }
}
